#include "input.h"
#include "include/config.h"

#if HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif

#include "ltgsql.h"

char* ltgsql_readline(const char* prompt) {
#if HAVE_READLINE
    return readline(prompt);
#else
    // 简单的回退实现
    printf("%s", prompt);
    fflush(stdout);

    char* buffer = NULL;
    size_t size = 0;
    ssize_t len = getline(&buffer, &size, stdin);

    if (len > 0) {
        if (buffer[len - 1] == '\n') {
            buffer[len - 1] = '\0';
        }
    }

    return buffer;
#endif
}

void ltgsql_add_history(const char* line) {
#if HAVE_READLINE
    add_history(line);
#endif
    // Windows 版本在另一个文件中实现
}

void ltgsql_save_history(const char* filename) {
#if HAVE_READLINE
    write_history(filename);
#endif
}

void ltgsql_load_history(const char* filename) {
#if HAVE_READLINE
    read_history(filename);
#endif
}